package ihm;

//usage : java ihm.Coord fichiermage.jpg

import graphes.Graphe;
import graphes.Noeud;



import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.xml.sax.SAXException;

import carte.AppliCarte;
import carte.Carte;
import carte.Voie;

public class MiniMappi extends JFrame {
	private static final long serialVersionUID = 1L;
	static final int DELTA = 20;
	int hauteurPlan, largeurPlan;
	// pour la vue scrollable de l'image
	protected ImageCanvas canvas = new ImageCanvas();
	protected ScrollPane mapView = new ScrollPane();
	protected JTextArea zoneTexte = new JTextArea(23, 40);
	final static boolean shouldFill = true;
	final static boolean shouldWeightX = true;
	final static boolean RIGHT_TO_LEFT = false;

	Carte G=null;
	Noeud ville1=null;
	Noeud ville2=null;
	Noeud villeTemp=null;
	JLabel label1=new JLabel("X");
	JTextField textX=new JTextField();
	JLabel label2=new JLabel("Y");
	JTextField textY=new JTextField();
	JLabel label3=new JLabel("NOM LIEU");
	JTextField textNomLieu=new JTextField();
	JButton depart=new JButton("setDepart");
	JTextField textDepart=new JTextField();
	JButton arrivee=new JButton("setArrivee");
	JTextField textArrivee=new JTextField();
    JRadioButton temps = new JRadioButton("plus rapide");
    JRadioButton distance = new JRadioButton("plus court");
    JRadioButton prix = new JRadioButton("moins cher");
    JButton goButton=new JButton("GO!!!");
	public MiniMappi(String fichierImage,String fichierGraphe) throws Exception {
		// chargement de l'image
		Image im = new ImageIcon(fichierImage).getImage();
		hauteurPlan = im.getHeight(null);
		largeurPlan = im.getWidth(null);
		// preparation de la vue scrollable de l'image
		canvas.setImage(im);
		canvas.addMouseListener(new CoordListener());
		mapView.setSize(hauteurPlan + DELTA, largeurPlan + DELTA);
		mapView.add(canvas);
		// construction de l'ensemble
		setLayout(new BorderLayout());
		add(mapView, BorderLayout.CENTER);
		JPanel south = new JPanel();
		add(south, BorderLayout.SOUTH);
		JPanel gauche = new JPanel();
		gauche.setSize(zoneTexte.getPreferredSize());
		JPanel droit = new JPanel();
		droit.add(zoneTexte);
		south.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		south.add(gauche, c);
		c.weightx = 0.5;
		c.gridx = 1;
		c.gridy = 0;
		c.gridwidth = 2; 
		south.add(droit, c);
		fillGauche(gauche);
		this.pack();
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				System.exit(0);
			}
		});
		
		G=AppliCarte.chargerGraphe(fichierGraphe);
		zoneTexte.setMinimumSize(zoneTexte.getSize());
		
	}
	private void fillGauche(JPanel gauche){


		gauche.setLayout(new BoxLayout(gauche, BoxLayout.Y_AXIS));

		gauche.add(label1);
		gauche.add(textX);
		gauche.add(label2);
		gauche.add(textY);
		gauche.add(label3);
		gauche.add(textNomLieu);
		gauche.add(depart);
		gauche.add(textDepart);
		gauche.add(arrivee);
		gauche.add(textArrivee);
	    temps.setSelected(true);
	    temps.addActionListener(new TypeRechercheListener());
	    temps.setActionCommand(""+Voie.TYPETEMPS);
	    distance.setActionCommand(""+Voie.TYPEDISTANCE);
	    distance.addActionListener(new TypeRechercheListener());
	    prix.setActionCommand(""+Voie.TYPEPRIX);
	    prix.addActionListener(new TypeRechercheListener());
	    //Group the radio buttons.
	    ButtonGroup group = new ButtonGroup();
	    group.add(temps);
	    group.add(distance);
	    group.add(prix);
	    gauche.add(temps);
	    gauche.add(distance);
	    gauche.add(prix);
	    gauche.add(goButton);
	    goButton.addActionListener(new ActionListener(){
	    	public void actionPerformed(ActionEvent arg) {
	    		ville1=G.findNoeud(textDepart.getText());
	    		ville2=G.findNoeud(textArrivee.getText());
	    		zoneTexte.setText(AppliCarte.meilleurChemin(G,ville1,ville2));
	    	}
	    });
	    depart.addActionListener(new ActionListener(){
	    	public void actionPerformed(ActionEvent arg) {
	    		textDepart.setText(textNomLieu.getText());
	    	}
	    });
	    arrivee.addActionListener(new ActionListener(){
	    	public void actionPerformed(ActionEvent arg) {
	    		textArrivee.setText(textNomLieu.getText());
	    	}
	    });


	}
	// classe locale de canvas
	class ImageCanvas extends Canvas {
		/**
	 * 
	 */
		private static final long serialVersionUID = 1L;
		Image image;

		void setImage(Image img) {
			image = img;
			setSize(largeurPlan, hauteurPlan);
			repaint();
		}

		public void paint(Graphics g) {
			// image
			// if (image!=null)
			// g.drawImage(image,DELTA,DELTA,hauteurPlan,largeurPlan,this);
			if (image != null)
				g.drawImage(image, DELTA, DELTA, this);
			// cadrillage
			int lignes = hauteurPlan / DELTA;
			int colonnes = largeurPlan / DELTA;
			g.setColor(Color.gray);
			for (int i = 1; i <= lignes; i++) {
				g.drawString("" + i, 0, (i + 1) * DELTA);
				g.drawLine(DELTA, i * DELTA, DELTA + largeurPlan, i * DELTA);
			}
			g.drawLine(DELTA, (lignes + 1) * DELTA, DELTA + largeurPlan,
					(lignes + 1) * DELTA);
			for (int i = 1; i <= colonnes; i++) {
				g.drawString("" + i, i * DELTA, DELTA / 2);
				g.drawLine(i * DELTA, DELTA, i * DELTA, DELTA + hauteurPlan);
			}
			g.drawLine((colonnes + 1) * DELTA, DELTA, (colonnes + 1) * DELTA,
					DELTA + hauteurPlan);
		}
	}

	// classe locale pour la vue des messages
	class CoordListener extends MouseAdapter{
		public void mouseReleased(MouseEvent e) {
			int x = e.getX() / DELTA;
			int y = e.getY() / DELTA;
			textX.setText("" + x);
			textY.setText("" + y);
			villeTemp=G.findByCoord(x,y);
			textNomLieu.setText("");
			if (villeTemp!=null){
				textNomLieu.setText(villeTemp.toString());
			}
			
		}
	}

	// lancement de l'IHM
	public static void main(String argv[])throws Exception {
		if (argv.length != 2)
			System.err.println("usage : java ihm.Coord fichiermage.jpg");
		else {
			MiniMappi w = new MiniMappi(argv[0],argv[1]);
			w.setTitle("MiniMapi");
			w.setSize(580, 800);
			w.setVisible(true);
			w.setResizable(false);
		}
	}
	

}

class TypeRechercheListener implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent arg) {
		Voie.typeRecherche=Integer.parseInt(arg.getActionCommand());
	}
	
}
